#!/usr/bin/envpython
# -*-coding:UTF-8-*-
'''
@File    :   error_utils.py
@Contact :   308711822@qq.com
@License :   (C) Copyright 2021-2225, Personal exclusive right.

@Modify Time      @Author    @Version    @Desciption
------------      -------    --------    -----------
2023/2/7 14:18   小钟同学      1.0         None
'''
import json
import re
from typing import Any, Callable, Dict, List, Pattern, Sequence, Union


class PydanticI18n:

    @classmethod
    def get_pydantic_messages(cls, output: str = "dict") -> Union[Dict[str, str], str]:
        output_mapping: Dict[str, Callable[[], Union[Dict[str, str], str]]] = {
            "json": cls._get_pydantic_messages_json,
            "dict": cls._get_pydantic_messages_dict,
            "babel": cls._get_pydantic_messages_babel,
        }
        return output_mapping[output]()

    @classmethod
    def _get_pydantic_messages_dict(cls) -> Dict[str, str]:
        from pydantic import errors

        messages = (
            re.sub(r"\{.+\}", "{}", getattr(errors, name).msg_template)
            for name in errors.__all__
            if hasattr(getattr(errors, name), "msg_template")
        )
        return {value: value for value in messages}

    @classmethod
    def _get_pydantic_messages_dict_code(cls) -> Dict[str, str]:
        from pydantic import errors

        messages = (
            "value_error." + re.sub(r"\{.+\}", "{}", getattr(errors, name).code)
            for name in errors.__all__
            if hasattr(getattr(errors, name), "code")
        )
        return {value: value for value in messages}

    @classmethod
    def _get_pydantic_messages_json(cls) -> str:
        return json.dumps(cls._get_pydantic_messages_dict(), indent=4)

    @classmethod
    def _get_pydantic_messages_babel(cls) -> str:
        return "\n\n".join(
            f'msgid "{item}"\nmsgstr "{item}"'
            for item in cls._get_pydantic_messages_dict()
        )


if __name__ == '__main__':
    print(PydanticI18n._get_pydantic_messages_dict_code())
